<?php


//form has been submitted
include '../connection.php';
include 'functions.php';
include 'index.php';
if ( isset( $_GET['lang'] ) )
	$lang = $_GET['lang'];
else
	$lang = 'en';
	
include 'lang/lang_'.$lang.'.php';

?>

<div id="body-wrapper">

    <div id="main-content">

        <div class="content-box">


<?php

if ( isset( $_GET['action'] ) )
	$action = $_GET['action'];
else
	$action = 'add';
	
	
$name_en	=	"";
$desc_en	=	"";
$status	 =	"";
$img		=	"";
$category   =	"";
$flabel = "";
$old_img = '';
$price = '';



//add, update
switch( $action )
{
	case 'add' :
		$flabel = "Save";
	
		if ( isset( $_POST['name_en'] ) )
		{
			
			$name_en	=	$_POST['name_en'];
			$desc_en	=	$_POST['desc_en'];
			$status	 =	$_POST['status'];
			$img		=	$_POST['img'];
			$category   =	$_POST['category'];
			$imageName = '1.jpg';
			$price = $_POST['price'];
			
			if ( isset( $_FILES['img']['name']) &&  $_FILES['img']['name'] != '')
			{
				$name  = $_FILES['img']['name'];
				$size = $_FILES['img']['size'];
				$type = $_FILES['img']['type'];
				$tmp_name = $_FILES['img']['tmp_name'];
				$width = '';
				$height = '';
				
				$imageName = imageUploading( $name, $type, $size, $tmp_name, $config['photo_path'] , $config['thumbnail_path'] , $width, $height);
			}
			
			$sysDate = date("y/m/d");
			$added_id = $_SESSION['id'];
			$query = "INSERT INTO products (name_en,desc_en,status,img,category,added_date,added_by,price)
			VALUES ( '".$name_en."', '".$desc_en."', '".$status."', '".$imageName."', '".$category."' , '".$sysDate."' , '".$added_id."','".$price."' )";
			
			if ( mysql_query($query) )
				echo 'Product has been added successfully<br/>';
		
		//echo $query;
		}

	
	break;
	
	case 'update' :
	$flabel = "Update";
	if ( isset( $_POST['name_en'] ) )
	{
		
		$name_en	=	$_POST['name_en'];
		$desc_en	=	$_POST['desc_en'];
		$status	 =	$_POST['status'];
		$img		=	$_POST['img'];
		$category   =	$_POST['category'];
		$old_img = $_POST['old_img'];
		$imageName = '1.jpg';
		$price = $_POST['price'];
			
		if ( isset( $_FILES['img']['name']) &&  $_FILES['img']['name'] != '')
		{
			$name  = $_FILES['img']['name'];
			$size = $_FILES['img']['size'];
			$type = $_FILES['img']['type'];
			$tmp_name = $_FILES['img']['tmp_name'];
			$width = '';
			$height = '';
			
			$imageName = imageUploading( $name, $type, $size, $tmp_name, $config['photo_path'] , $config['thumbnail_path'] , $width, $height);
		}else
		{
			$imageName = $old_img;
		}
			
		
		$query = "UPDATE PRODUCTS SET  name_en= '".$name_en."', desc_en ='".$desc_en."', status = '".$status."',
		category= '".$category."', img = '".$imageName."' , price ='".$price."' WHERE ID = '".$_GET['id']."'";
		if ( mysql_query($query) )
			echo 'row has been updated successfully<br/>';
			?>
            
            <script>
				window.location.href = 'viewAllProducts.php';
			</script>
            <?php
	}
	
	break;
	
	case 'delete' :
	
		$query = "DELETE FROM products WHERE id = " . $_GET['id'];
		mysql_query($query);
		
		?> 
           <script>
				window.location.href = 'viewAllProducts.php';
			</script>
        <?php
			
	break;
}

if ( $action == 'update' )
{
	$id = $_GET['id'];
	$query = "SELECT * FROM PRODUCTS WHERE ID = " . $id;
	$res = mysql_query($query);
	
	if ( mysql_affected_rows() == '1' )
	{
		$row = mysql_fetch_array($res);
		
			
		
		$name_en	=	$row['name_en'];
		$desc_en	=	$row['desc_en'];
		$status	 =	$row['status'];
		$img		=	$row['img'];
		$category   =	$row['category'];
		$old_img   =	$row['img'];
		$price = $row['price'];
		
		
		//echo $category;
	}
}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style_<?= $lang ?>.css" media="screen" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add new product</title>
</head>

<body>
 <div class="content-box-header" id = "addProducts" >

                <h3><?= Add_Product ?></h3>

                <div class="clear"></div>
            </div>
  <div class="content-box-content">

                <div class="tab-content default-tab" id="tab2">
<!-- enctype="multipart/form-data" for uploading images thriugh the form -->
<form action="" method="post" enctype="multipart/form-data" class="text-input small-input">
	<input type="hidden" name='old_img' id="old_img" value="<?= $old_img ?>"  />
	
    
    
    <label><?= name_en_lb ?></label>
    <input type="text" class="text-input small-input"  id="name_en"  name="name_en" value="<?= $name_en ?>" />
    <br />
    <br />
 
    <label><?= desc_en_lb ?></label>
    <!--<input type="" id="desc_en" name="desc_en"  />-->
    <textarea id="desc_en" class="text-input small-input" name="desc_en" ><?= $desc_en ?></textarea>
    <br />
    
 <label><?= price_lb ?></label>
    <input type="text" class="text-input small-input"  id="price"  name="price" value="<?= $price ?>" />
    <br />
    <br />
 	
    <label><?= status_lb ?></label>
    <!--<input type="" id="status" name="status"  />-->
    
    <input type="radio" value="0" id="status" name="status" <?php if ( $status == '0' ) echo 'checked="checked"'  ?> />
    DEActive

    <input type="radio" value="1" id="status" name="status" <?php if ( $status == '1' ) echo 'checked="checked"'  ?> />
    Active
    
    <br />
    <br />
    <label><?= category_lb ?></label>
    <select name="category" id="category">
    	<option value="1" <?php if ( $category == 1 ) echo 'selected="selected"' ?>>Samsung</option>
        <option value="2" <?php if ( $category == 2 ) echo 'selected="selected"' ?>>Nokia</option>
        <option value="3" <?php if ( $category == 3 ) echo 'selected="selected"' ?>>Apple</option>
        <option value="4" <?php if ( $category == 4 ) echo 'selected="selected"' ?> >Sony</option>
    </select><br /><br />
    
    <label><?= img_lb ?></label>
    <input type="file" id="img" name="img" />
    <br />
    <br />
    <input type="submit" class="button" value="<?= $flabel?>" />    
</form>
     </div>
    </div>
</div>

                </div>
            </div>
</body>
</html>